﻿Imports System.ComponentModel
Imports DevExpress.LookAndFeel
Imports System.Linq


Public Class FLogin
    Sub New()
        InitSkins()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
    End Sub
    Sub InitSkins()
        DevExpress.Skins.SkinManager.EnableFormSkins()
        DevExpress.UserSkins.BonusSkins.Register()
        UserLookAndFeel.Default.SetSkinStyle("DevExpress Style")
    End Sub

    Private Sub ButtonLogin_Click(sender As Object, e As EventArgs) Handles ButtonLogin.Click
        'DECLARAR BASE "LinqMain"
        Dim db As New LinqMainDataContext
        TextUsername.BackColor = Color.Transparent
        TextPassword.BackColor = Color.Transparent
        'Processo de Validação de Preechimento de Campos
        If TextUsername.Text = "" And TextPassword.Text = "" Then
            TextUsername.BackColor = Color.OrangeRed
            TextPassword.BackColor = Color.OrangeRed
        ElseIf TextUsername.Text = "" And TextPassword.Text <> "" Then
            TextUsername.BackColor = Color.OrangeRed
            TextPassword.BackColor = Color.OrangeRed
            TextUsername.Focus()
            TextPassword.Text = ""
        ElseIf TextUsername.Text <> "" And TextPassword.Text = "" Then
            TextPassword.BackColor = Color.OrangeRed
            TextPassword.Focus()
            'Validaçao de nome de utilizador e pass
        ElseIf TextUsername.Text <> "" And TextPassword.Text <> "" Then
            GlobalVariables.UserName = TextUsername.Text

            Dim CheckUser = (From c In db.tblUsers _
                             Where c.UserName = GlobalVariables.UserName AndAlso c.Pass = TextPassword.Text _
                             Select c).Count
            If CheckUser = 0 Then
                MessageBox.Show("UserName ou Password Errados!", "Erro Login", MessageBoxButtons.OK, MessageBoxIcon.Error)
                TextPassword.Text = ""
            ElseIf CheckUser = 1 Then
                Dim emp = (From c In db.tblUsers Join o In db.tblEmpresa1s On c.EmpresaDefault Equals o.EmpresaDesc _
                             Where c.UserName = GlobalVariables.UserName _
                             Select o).SingleOrDefault
                GlobalVariables.EmpresaDesc = emp.EmpresaDesc
                GlobalVariables.Empresa = emp.Empresa

                Dim Unegocio = (From c In db.tblUsers _
                                Where c.UserName = GlobalVariables.UserName _
                                Select c.un).SingleOrDefault

                GlobalVariables.Un = Unegocio

                Dim VendedorCod = (From c In db.tblUsers _
                                Where c.UserName = GlobalVariables.UserName _
                                Select c.Vendedor).SingleOrDefault

                GlobalVariables.Vendedor = VendedorCod
                FMain.Show()
            End If
        End If

    End Sub

    Private Sub FLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class

Public Class GlobalVariables
    Public Shared UserName As String
    Public Shared Empresa As String
    Public Shared EmpresaDesc As String
    Public Shared Un As String
    Public Shared Vendedor As String
End Class